package cn.b.sky.mes.cm.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.cm.dao.WorkCenterDao;
import cn.b.sky.mes.cm.dao.WorkCenterTypeDao;
import cn.b.sky.mes.cm.model.WorkCenter;
import cn.b.sky.mes.cm.model.WorkCenterType;
import cn.b.sky.mes.cm.model.WorkUnit;
import cn.b.sky.mes.cm.model.condition.WorkCenterCondition;
import cn.b.sky.mes.cm.service.WorkCenterService;
import cn.b.sky.common.model.TreeInterface;
import cn.b.sky.common.utils.TreeUtil;

@Service("workCenterService")
public class WorkCenterServiceImpl implements WorkCenterService {

  @Autowired
  private WorkCenterDao workCenterDao;

  @Autowired
  private WorkCenterTypeDao workCenterTypeDao;

  @Override
  public List<TreeInterface> getWorkCenterTree(String siteCode) {
    List<WorkCenter> workCenters = getAllWorkCenterListBySiteCode(siteCode, null);

    return TreeUtil.gradingRecursion(workCenters.toArray(new WorkCenter[workCenters.size()]), null);
  }

  @Override
  public List<WorkCenter> getAllWorkCenterListBySiteCode(String siteCode, Integer lockFlag) {
    return workCenterDao.selectAllWorkCenter(siteCode, lockFlag);
  }

  @Override
  public List<WorkCenter> getWorkCenterListByCondition(WorkCenter workCenter) {
    if (workCenter == null) {
      return new ArrayList<WorkCenter>();
    } else {
      return workCenterDao.selectWorkCenterByCondition(workCenter);
    }
  }

  @Override
  public void saveWorkCenter(WorkCenter workCenter) {
    if (isCheckParam(workCenter)) {
      this.workCenterDao.insertWorkCenter(workCenter);
    }
  }

  @Override
  public void modifyWorkCenter(WorkCenter workCenter) {
    if (isCheckParam(workCenter)) {
      this.workCenterDao.updateWorkCenter(workCenter);
    }
  }

  @Override
  public void deleteWorkCenter(WorkCenter workCenter) {
    if (isCheckParam(workCenter)) {
      this.workCenterDao.deleteWorkCenter(workCenter.getSiteCode(), workCenter.getWorkCenterCode());
    }

  }

  /**
   * 
   * <strong>Title : isCheckParam<br>
   * </strong> <strong>Description : </strong>参数判断<br>
   * <strong>Create on : 2015-9-24 下午3:51:54<br>
   * </strong>
   * <p>
   * 
   * @param workCenter
   * @return boolean
   * @throws
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   *          <strong>修改历史:</strong><br>
   *          修改人 | 修改日期 | 修改描述<br>
   *          -------------------------------------------<br>
   * <br>
   * <br>
   */
  public boolean isCheckParam(WorkCenter workCenter) {
    if (workCenter == null) {
      return false;
    }
    if (StringUtils.isBlank(workCenter.getSiteCode())) {
      return false;
    }
    if (StringUtils.isBlank(workCenter.getWorkCenterCode())) {
      return false;
    }
    return true;
  }

  @Override
  public WorkCenter getWorkCenterByPK(String siteCode, String workCenterCode) {
    return workCenterDao.getWorkCenterByPK(siteCode, workCenterCode);
  }

  @Override
  public List<WorkCenterType> getSameLevelTypeByWorkCenter(String siteCode, String parentWorkCenterCode) {
    if (StringUtils.isBlank(siteCode)) {
      return new ArrayList<WorkCenterType>();
    }
    if (StringUtils.isBlank(parentWorkCenterCode)) {
      return workCenterTypeDao.selectWorkCenterTypesByParentCode(siteCode, null);
    }
    WorkCenter wc = getWorkCenterByPK(siteCode, parentWorkCenterCode);
    if (wc != null) {
      return workCenterTypeDao.selectWorkCenterTypesByParentCode(siteCode, wc.getWorkCenterTypeCode());
    }
    return new ArrayList<WorkCenterType>();
  }

  @Override
  public List<WorkCenter> getAllWorkCenterListByProcessCode(String processCode, String siteCode, String processrev) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(processCode)) {
      return new ArrayList<WorkCenter>();
    }
    return workCenterDao.getWorkCentersListByProcessCode(processCode, siteCode, processrev);
  }

  @Override
  public List<WorkUnit> selectWorkUnitByWorkCenter(String siteCode, String workCenterCode) {
    return workCenterDao.selectWorkUnitByWorkCenter(siteCode, workCenterCode);
  }

  @Override
  public List<WorkCenter> getWorkCenterSuggest(WorkCenterCondition workCenter) {

    return workCenterDao.getWorkCenterSuggest(workCenter);
  }

  @Override
  public List<WorkCenter> getWorkCenterListByCondition(WorkCenterCondition condition) {
    if (condition == null) {
      condition = new WorkCenterCondition();
    }
    return workCenterDao.getWorkCenterListByCondition(condition);
  }

  @Override
  public List<WorkCenter> getWorkCenterList(WorkCenter workCenter) {
    if (workCenter == null) {
      return new ArrayList<WorkCenter>();
    } else {
      return workCenterDao.selectWorkCenterByCondition(workCenter);
    }
  }

  @Override
  public List<WorkCenter> getWorkCenterLookUp(WorkCenterCondition condition) {
    return workCenterDao.getWorkCenterLookUp(condition);
  }

  @Override
  public int getWorkCenterLookUpCount(WorkCenterCondition condition) {
    return workCenterDao.getWorkCenterLookUpCount(condition);
  }

  @Override
  public WorkCenter getWorkCenterByPKandPermission(String siteCode, String workCenterCode, String userCode) {
    return workCenterDao.getWorkCenterByPKandPermission(siteCode, workCenterCode, userCode);
  }

}
